package com.handpay.zztong.hp.login.model.login;
import android.content.DialogInterface;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.fastjson.FastJsonHelper;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.base.BaseModel;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.HttpUrlConstant;
import com.handpay.zztong.hp.utils.AccountUtils;
import java.util.Hashtable;
import se.krka.kahlua.vm.LuaTable;

/**
 * Created by lyang on 2017/3/15.
 */

public class LoginModel extends BaseModel {
    private BusinessEngineListener mAccountLoginBusinessEngineListener;
    private BusinessEngineListener mGetLoginVerificationCodeBusinessEngineListener;


    /**
     * 10.9. 获取验证码
     * @param activity
     * @param params
     * @param listener
     * @return
     */
    public boolean doGetLoginVerificationCodePost(ZZTongActivity activity, Hashtable<String, String> params, BusinessEngineListener listener) {
        mGetLoginVerificationCodeBusinessEngineListener = listener;
        return doPost(activity, HttpUrlConstant.Do_GetLoginVerificationCode, params, true, true);
    }


    /**
     * 10.9. 账账通用户使用
     * @param activity
     * @param params
     * @param listener
     * @return
     */
    public boolean doAccountLoginPost(ZZTongActivity activity, Hashtable<String, String> params, BusinessEngineListener listener) {
        mAccountLoginBusinessEngineListener = listener;
        return doPost(activity, HttpUrlConstant.Do_AccountLogin, params, true, true);
    }

    @Override
    public boolean netResponse(String action,byte data[]) {
        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                AccountUtils.removeRMSPWDData();
                dialog.dismiss();
            }
        };
        boolean bResult = super.netResponse(action, data, listener);
        if (bResult) {
            return true;
        }
        if (action.equals(HttpUrlConstant.Do_AccountLogin)) {
            AccountLoginBean bean = null;
            if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
                try {
                    String result = new String(data, "UTF-8");
                    bean = FastJsonHelper.getObject(result, AccountLoginBean.class);
                    String account = SecureManager.getInstance().des(bean.getAccount(), 0, null);
                    String phone = SecureManager.getInstance().des(bean.getPhone(), 0, null);
                    String  shopName = SecureManager.getInstance().des(bean.getShopName(), 0, null);
                    bean.setAccount(account);
                    bean.setPhone(phone);
                    bean.setShopName(shopName);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, true);
                String account = (String) luaTable.rawget("account");
                String phone = (String) luaTable.rawget("phone");
                String status = (String) luaTable.rawget("status");
                String shopName = (String) luaTable.rawget("shopName");
                account = SecureManager.getInstance().des(account, 0, null);
                phone = SecureManager.getInstance().des(phone, 0, null);
                shopName = SecureManager.getInstance().des(shopName, 0, null);
                String failDescription = (String) luaTable.rawget("failDescription");
                String merchantCode = (String) luaTable.rawget("merchantCode");
                String status_obj = (String) luaTable.rawget("passwdStatus");
                bean = new AccountLoginBean();
                bean.setAccount(account);
                bean.setPhone(phone);
                bean.setStatus(status);
                bean.setShopName(shopName);
                bean.setFailDescription(failDescription);
                bean.setMerchantCode(merchantCode);
                bean.setPasswdStatus(status_obj);
            }
            if (mAccountLoginBusinessEngineListener != null) {
                mAccountLoginBusinessEngineListener.callbackBusinessResponse(bean);
            }
        }else if (action.equals(HttpUrlConstant.Do_GetLoginVerificationCode)) {
            LoginVerficationCodeBean bean = null;
            if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
                try {
                    String result = new String(data, "UTF-8");
                    bean = FastJsonHelper.getObject(result, LoginVerficationCodeBean.class);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {

            }
            if (mGetLoginVerificationCodeBusinessEngineListener != null) {
                mGetLoginVerificationCodeBusinessEngineListener.callbackBusinessResponse(bean);
            }
        }

        return true;
    }

    @Override
    public void callbackJsonNetResponse(String action, ResponseBean resp, boolean showAlert) {

    }
}
